// stores/counter.js
import { defineStore } from 'pinia';
export const useSettingStore = defineStore('setting', {
	// 开启数据持久化
	persist: true,
	state: () => {
		return {
			activePath: '/manage/index/manager',
			StatusBar: 0,
			CustomBar: 0,
			Custom: {},
			serviceRange: false,
			ColorList: [{
				title: '嫣红',
				name: 'red',
				color: '#e54d42'
			}],
			userInfo: {
				"id": "",
				"username": "",
				"realName": "",
				"nickname": "游客",
				"avatarUrl": "",
				"phone": "",
				"address": "",
				"email": "",
				"birthday": "",
				"gender": 1,
				"roleKeys": [],
				"msgNum": "0",
				"surplus": "",
			},
			isLogin: false,
			isBindAddress: false,
			orderType: 'takein',
			roleType: '',
			routerList: [],
			platform:'ios',
			addresses: [{
				"id": 1,
				"user_id": 1,
				"name": "梁先生",
				"phone": "18666610100",
				"gender": 0,
				"address": "有间大厦",
				"complete_address": "广东省深圳市宝安区福海大道118号",
				"description": "ABC1234",
				"latitude": "",
				"longitude": "",
				"is_default": 1
			}],
			token: '',
			openId: ''
		};
	},
	getters: {

	},
	actions: {
		SET_ORDER_TYPE(orderType : any) {
			this.orderType = orderType
		},
		SET_USERINFO(userInfo : any) {
			this.userInfo = userInfo
		},
		SET_ISLOGIN(isLogin : any) {
			this.isLogin = isLogin
		},

		onLogin(data : string) {
			this.token = data
			try {
				uni.setStorageSync('token', data);
				uni.setStorage({
					key: 'token',
					data: data
				})
			} catch (e) {
				// error
			}
		}, setToken(value : string) {
			if (value) {
				this.token = value
			}
		}, setOpenId(value : string) {
			if (value) {
				this.openId = value
				uni.setStorageSync('openId', value);
				uni.setStorage({
					key: 'openId',
					data: value
				})
			}
		},
		getToken() {
			try {
				const value = uni.getStorageSync('token');
				if (value) {
					this.token = value
				} else {
					this.token = ''
				}
				uni.getStorage({
					key: 'userinfo',
					success: (res : any) => {
						this.userInfo = JSON.parse(res.data);
					},
					fail: () => {

					}
				})
				

			} catch (e) {
				// error
				this.token = ''
			}
			return this.token
		},
		logout() {
			this.token = ''
			try {
				uni.removeStorageSync('token');
				uni.removeStorage({
					key: 'token'
				})
				this.userInfo = {
					"id": "",
					"username": "",
					"realName": "",
					"nickname": "游客",
					"avatarUrl": "",
					"phone": "",
					"address": "",
					"email": "",
					"birthday": "",
					"gender": 1,
					"roleKeys": [],
					"msgNum": "0",
					"surplus": "",
				};
			} catch (e) {
				// error
			}
		}
	}
});